package com.wing.tools.streamserver;

import android.content.Context;
import android.opengl.EGL14;

import com.wing.tools.screenlive.encoder.TextureEncoder;


/**
 * Created by wejust on 2018/1/4.
 */

public class TextureLiveServer {
    TextureEncoder mEncoder;
    int mTextureID;
    StreamServer mStreamSvr;
    Context mContext;

    public TextureLiveServer(Context ctx, int texID)
    {
        this.mTextureID = texID;

        mStreamSvr = new StreamServer();
        mContext = ctx;
    }

    public void config(int port,String appName, String mediaName, int width, int height, int frameInterval, int videoBitrate, int fps)
    {
        mEncoder = new TextureEncoder(mContext, mStreamSvr);
        mEncoder.config(frameInterval, videoBitrate, width, height, fps);
        mEncoder.init(EGL14.eglGetCurrentContext());
        mEncoder.setTextureID(this.mTextureID);

        mStreamSvr.init(port, appName, mediaName, width, height, videoBitrate);
    }

    public void start(){
        mStreamSvr.start();
        mEncoder.begin();
    }

    public int getTextureID(){
        if(mEncoder != null) {
            return mEncoder.getTextureID();
        }
        return -1;
    }

    public void drawFrame(int texID){
        if(mEncoder != null && mEncoder.isAlive()) {
            this.mEncoder.drawFrame(texID);
        }
    }

    public void stop()
    {
        if(mEncoder != null) {
            mEncoder.end();
        }
        mStreamSvr.stop();
    }
}
